<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%@include file="../base/base.jspf" %>
<div class="layui-form" style="padding: 47px 0 0 70px;">
    <input type="hidden" id="userId" name="usrId" value="${user.usrId}"/>
    <div class="layui-col-xs12">
        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-inline">
                <input type="password" id="oldPassword" autocomplete="off" lay-verify="required|isOldPassword"
                       placeholder="请输入原密码" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" id="newPassword" autocomplete="off" lay-verify="required|noAsOldPassword"
                       placeholder="请输入新密码" class="layui-input"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" autocomplete="off" lay-verify="required|sameAsNewPassword"
                       name="password" placeholder="请输入确认密码" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit id="password-front-submit" lay-filter="password-front-submit" value="提交"/>
    </div>
</div>
<script type="text/javascript">
    layui.config({
        base: "${ctx}/static/framework/layuiadmin/" //静态资源所在路径
    }).extend({
        index: "lib/index" //主入口模块
    }).use(["index", "form"], function () {
        var form = layui.form;
        var id = $("#userId").val();
        form.verify({
            isOldPassword: function (value, item) { //value：表单的值、item：表单的DOM对象
                var content = null;
                $.ajax({
                    async: false,
                    url: "${ctx}/isOldPassword",
                    data: {
                        id: id,
                        password: value
                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (!data) {
                            content = "您输入的密码与原密码不一致";
                        }
                    }
                });
                if (content) {
                    return content;
                }
            },
            noAsOldPassword: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ($("#oldPassword").val() == value) {
                    return "您输入的新密码与原密码一致";
                }
            },
            sameAsNewPassword: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ($("#newPassword").val() != value) {
                    return "您输入的确认密码与新密码不一致";
                }
            }
        });
        form.on("submit(password-front-submit)", function (data) {
            var field = data.field;
            $.ajax({
                async: true,
                url: "${ctx}/user/changPassword",
                type: "post",
                dataType: "json",
                data: field,
                success: function (data) {
                    if (data.result) {
                        $.ajax({
                            async: true,
                            type: "post",
                            url: "${ctx}/loginOut",
                            dataType: "json",
                            success: function (json) {
                                success(json.info);
                                setTimeout(function () {
                                    parent.location.href = "${ctx}/login";
                                }, 2000);
                            }
                        });
                    } else {
                        error(data.info);
                    }
                }
            });
        });
    });
</script>